package it.polimi.swKnights.SWIMv2.servlets.feedback;

import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.NoMatchingHelpRequest;
import it.polimi.swKnights.SWIMv2.ejb.feedbacks.LeaveFeedbackHandlerRemote;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LeaveFeedbackProceed
 */
public class LeaveFeedbackProceed extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String userName = (String) request.getSession()
				.getAttribute("userName");
		String password = (String) request.getSession()
				.getAttribute("password");

		String gotBy = request.getParameter("gotBy");
		int id = Integer.parseInt(request.getParameter("id"));

		Integer rate = 0;
		try
		{
			rate = Integer.parseInt(request.getParameter("rate"));
		}
		catch (Exception e)
		{
			rate = 0;
		}

		String text = request.getParameter("feedback_text");

		LeaveFeedbackHandlerRemote leaveHand = this
				.lookUp(LeaveFeedbackHandlerRemote.class);

		try
		{
			leaveHand.leaveFeedback(id, gotBy, rate, text, userName, password);
			response.sendRedirect("./leave-feedback");
		}
		catch (LoginFailException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.session);
		}
		catch (InvalidLoginPrivilegesException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.session);
		}
		catch (NoMatchingHelpRequest e)
		{
			response.sendRedirect("./leave-feedback?error=" + Errors.nomatchinghelp);
		}

	}

}
